package com.wang.tool.okhttp;

import com.wang.tool.util.SimpleLogUtil;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

/**
 * okhttp配置信息
 *
 * @author wangshaopeng
 * @date 2020/8/28
 */
public class OkHttpConfig implements Serializable {
    private static final long serialVersionUID = 1L;

    private String address;
    private Integer port;
    private String username;
    private String password;
    private Proxy.Type proxyType = Proxy.Type.HTTP;

    private int connectTimeout;

    private int readTimeout;

    private int writeTimeout;


    /**
     * 新创建初始化okHttpClient
     *
     * @return
     */
    public static OkHttpClient newOkClient(OkHttpConfig config) {
        SimpleLogUtil.info("[initOkClientStart]");
        // new Client
        OkHttpClient.Builder clientBuilder = HttpTemplate.autoBuilder();
        // 模板默认属性给他
        // 自定义配置
        if (config != null) {
            // 代理
            Proxy proxy = config.getProxy();
            if (proxy != null) {
                clientBuilder.proxy(proxy);
                // 设置授权
                clientBuilder.authenticator((route, response) -> {
                    String credential = Credentials.basic(config.getUsername(), config.getPassword());
                    return response.request().newBuilder().header("Authorization", credential).build();
                });
            }
            // 超时
            if (config.getConnectTimeout() > 0) {
                clientBuilder.connectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS);
            }
            if (config.getReadTimeout() > 0) {
                clientBuilder.readTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS);
            }
            if (config.getWriteTimeout() > 0) {
                clientBuilder.writeTimeout(config.getWriteTimeout(), TimeUnit.MILLISECONDS);
            }
            // 其他属性添加后再适配...

        }
        SimpleLogUtil.info("[initOkClientFinished]");
        return clientBuilder.build();
    }

    /**
     * 获取代理对象
     *
     * @return 如果有代理地址, 就返会代理属性, 如果没有, 就返回null
     */
    public Proxy getProxy() {
        if (address != null) {
            return new Proxy(proxyType, new InetSocketAddress(address, port));
        } else {
            return null;
        }
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Proxy.Type getProxyType() {
        return proxyType;
    }

    public void setProxyType(Proxy.Type proxyType) {
        this.proxyType = proxyType;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public int getWriteTimeout() {
        return writeTimeout;
    }

    public void setWriteTimeout(int writeTimeout) {
        this.writeTimeout = writeTimeout;
    }

    @Override
    public String toString() {
        return "OkHttpConfig{" +
                "address='" + address + '\'' +
                ", port=" + port +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", proxyType=" + proxyType +
                ", connectTimeout=" + connectTimeout +
                ", readTimeout=" + readTimeout +
                ", writeTimeout=" + writeTimeout +
                '}';
    }
}
